package handlers

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"github.com/yourusername/virt-manager-go/internal/models"
	"github.com/yourusername/virt-manager-go/internal/service"
)

// SystemHandler handles system-level requests
type SystemHandler struct {
	connMgr *service.ConnectionManager
	log     *logrus.Logger
}

// NewSystemHandler creates a new system handler
func NewSystemHandler(connMgr *service.ConnectionManager, log *logrus.Logger) *SystemHandler {
	return &SystemHandler{
		connMgr: connMgr,
		log:     log,
	}
}

// GetSystemInfo returns hypervisor system information
//
//	@Summary		Get system information
//	@Description	Returns detailed hypervisor system information for a specific connection
//	@Tags			system
//	@Accept			json
//	@Produce		json
//	@Param			connection	query		string	true	"Connection ID"
//	@Success		200			{object}	models.SystemInfo
//	@Failure		400			{object}	map[string]interface{}	"Bad Request"
//	@Failure		500			{object}	map[string]interface{}	"Internal Server Error"
//	@Router			/system/info [get]
func (h *SystemHandler) GetSystemInfo(c *gin.Context) {
	conn, err := getConnectionFromRequest(c, h.connMgr)
	if err != nil {
		h.log.WithError(err).Error("Failed to get connection")
		c.JSON(http.StatusInternalServerError, gin.H{"error": "connection not available"})
		return
	}

	hostname, err := conn.GetHostname()
	if err != nil {
		h.log.WithError(err).Error("Failed to get hostname")
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	libvirtVersion, err := conn.GetVersion()
	if err != nil {
		h.log.WithError(err).Error("Failed to get libvirt version")
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	hvConn := conn.GetConnect()
	hvVersion, err := hvConn.GetVersion()
	if err != nil {
		h.log.WithError(err).Error("Failed to get hypervisor version")
		hvVersion = 0
	}

	uri, err := hvConn.GetURI()
	if err != nil {
		h.log.WithError(err).Error("Failed to get URI")
		uri = "unknown"
	}

	info := models.SystemInfo{
		Hostname:          hostname,
		LibvirtVersion:    libvirtVersion,
		HypervisorVersion: hvVersion,
		URI:               uri,
	}

	c.JSON(http.StatusOK, info)
}

// GetCapabilities returns host capabilities
//
//	@Summary		Get host capabilities
//	@Description	Returns XML capabilities document for a specific connection
//	@Tags			system
//	@Accept			json
//	@Produce		xml
//	@Param			connection	query		string					true	"Connection ID"
//	@Success		200			{string}	string					"XML capabilities document"
//	@Failure		400			{object}	map[string]interface{}	"Bad Request"
//	@Failure		500			{object}	map[string]interface{}	"Internal Server Error"
//	@Router			/system/capabilities [get]
func (h *SystemHandler) GetCapabilities(c *gin.Context) {
	conn, err := getConnectionFromRequest(c, h.connMgr)
	if err != nil {
		h.log.WithError(err).Error("Failed to get connection")
		c.JSON(http.StatusInternalServerError, gin.H{"error": "connection not available"})
		return
	}

	caps, err := conn.GetCapabilities()
	if err != nil {
		h.log.WithError(err).Error("Failed to get capabilities")
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.Data(http.StatusOK, "application/xml", []byte(caps))
}
